﻿using Cinch;
using FR.SAFTAnalyser.Model;
using MEFedMVVM.Common;
using System;
using System.Linq;

namespace FR.SAFTAnalyser.ViewModels
{
    public class GeneralLedgerEntriesViewModel : WorkspaceViewModel
    {
        public GeneralLedgerEntriesViewModel()
        {
            if (!Designer.IsInDesignMode)
            {
                Mediator.Instance.Register(this);
                this.DisplayName = "Movimentos";
            }
        }

        [MediatorMessageSink(MessageType.SAFT_FILE_OPENED)]
        private void LoadGeneralLedgerEntries(AuditFile saft)
        {
            if (saft.GeneralLedgerEntries != null)
            {
                this.GeneralLedgerEntriesJournal = saft.GeneralLedgerEntries.Journal;

                NumberOfEntries = saft.GeneralLedgerEntries.NumberOfEntries;
                TotalCredit = saft.GeneralLedgerEntries.TotalCredit.ToString("c");
                TotalDebit = saft.GeneralLedgerEntries.TotalDebit.ToString("c");
            }
        }

        GeneralLedgerEntriesJournal[] generalLedgerEntriesJournal;
        public GeneralLedgerEntriesJournal[] GeneralLedgerEntriesJournal
        {
            get
            {
                if (generalLedgerEntriesJournal == null && SAFT.SaftAnalyser.Instance.SaftFile != null && SAFT.SaftAnalyser.Instance.SaftFile.GeneralLedgerEntries != null)
                {
                    generalLedgerEntriesJournal = SAFT.SaftAnalyser.Instance.SaftFile.GeneralLedgerEntries.Journal;

                    NumberOfEntries = SAFT.SaftAnalyser.Instance.SaftFile.GeneralLedgerEntries.NumberOfEntries;
                    TotalCredit = SAFT.SaftAnalyser.Instance.SaftFile.GeneralLedgerEntries.TotalCredit.ToString("c");
                    TotalDebit = SAFT.SaftAnalyser.Instance.SaftFile.GeneralLedgerEntries.TotalDebit.ToString("c");
                }
                return generalLedgerEntriesJournal;
            }
            set
            {
                generalLedgerEntriesJournal = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.GeneralLedgerEntriesJournal));

                DocNumberOfEntries = generalLedgerEntriesJournal.Length;
                DocTotalCredit = generalLedgerEntriesJournal
                                         .Sum(c => c.Transaction.Sum(t => t.Line.Sum(l => l.ItemElementName == ItemChoiceType3.CreditAmount ? Math.Round(l.Item, 2, MidpointRounding.AwayFromZero) : 0)))
                                         .ToString("c");

                DocTotalDebit = generalLedgerEntriesJournal
                                        .Sum(c => c.Transaction.Sum(t => t.Line.Sum(l => l.ItemElementName == ItemChoiceType3.DebitAmount ? Math.Round(l.Item, 2, MidpointRounding.AwayFromZero) : 0)))
                                        .ToString("c");

                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.DocNumberOfEntries));
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.DocTotalCredit));
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.DocTotalDebit));

                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.NumberOfEntries));
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.TotalCredit));
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.TotalDebit));
            }
        }

        GeneralLedgerEntriesJournal journal;
        public GeneralLedgerEntriesJournal Journal
        {
            get { return journal; }
            set
            {
                journal = value;
                if (journal != null)
                    this.Transactions = journal.Transaction;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.Journal));
            }
        }

        GeneralLedgerEntriesJournalTransaction[] transactions;
        public GeneralLedgerEntriesJournalTransaction[] Transactions
        {
            get { return transactions; }
            set
            {
                transactions = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.Transactions));
            }
        }

        GeneralLedgerEntriesJournalTransaction transaction;
        public GeneralLedgerEntriesJournalTransaction Transaction
        {
            get { return transaction; }
            set
            {
                transaction = value;
                if (transaction != null)
                    this.TransactionLines = transaction.Line;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.Transaction));
            }
        }

        GeneralLedgerEntriesJournalTransactionLine[] transactionLines;
        public GeneralLedgerEntriesJournalTransactionLine[] TransactionLines
        {
            get { return transactionLines; }
            set
            {
                transactionLines = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<GeneralLedgerEntriesViewModel>(x => x.TransactionLines));
            }
        }

        public int DocNumberOfEntries { get; set; }

        public string DocTotalCredit { get; set; }

        public string DocTotalDebit { get; set; }


        public string NumberOfEntries { get; set; }

        public string TotalCredit { get; set; }

        public string TotalDebit { get; set; }

        string filterGeneralLedgerEntriesJournal;
        public string FilterGeneralLedgerEntriesJournal
        {
            get { return filterGeneralLedgerEntriesJournal; }
            set
            {
                filterGeneralLedgerEntriesJournal = value;

                if (string.IsNullOrEmpty(filterGeneralLedgerEntriesJournal))
                    this.GeneralLedgerEntriesJournal = SAFT.SaftAnalyser.Instance.SaftFile.GeneralLedgerEntries.Journal;
                else
                {
                    this.GeneralLedgerEntriesJournal =
                        (from c in SAFT.SaftAnalyser.Instance.SaftFile.GeneralLedgerEntries.Journal
                         where !string.IsNullOrEmpty(c.Description) && c.Description.IndexOf(filterGeneralLedgerEntriesJournal, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             !string.IsNullOrEmpty(c.JournalID) && c.JournalID.IndexOf(filterGeneralLedgerEntriesJournal, System.StringComparison.OrdinalIgnoreCase) >= 0
                         select c).ToArray();
                }
            }
        }

        string filterTransactions;
        public string FilterTransactions
        {
            get { return filterTransactions; }
            set
            {
                filterTransactions = value;

                if (string.IsNullOrEmpty(filterTransactions))
                    this.Transactions = Journal.Transaction;
                else
                {
                    this.Transactions =
                        (from c in Journal.Transaction
                         where !string.IsNullOrEmpty(c.Description) && c.Description.IndexOf(filterTransactions, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             !string.IsNullOrEmpty(c.DocArchivalNumber) && c.DocArchivalNumber.IndexOf(filterTransactions, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             !string.IsNullOrEmpty(c.Period) && c.Period.IndexOf(filterTransactions, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             !string.IsNullOrEmpty(c.SourceID) && c.SourceID.IndexOf(filterTransactions, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             !string.IsNullOrEmpty(c.TransactionID) && c.TransactionID.IndexOf(filterTransactions, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             c.TransactionDate.ToString("yyyy-MM-ddTHH:mm:ss").IndexOf(filterTransactions, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             c.GLPostingDate.ToString("yyyy-MM-ddTHH:mm:ss").IndexOf(filterTransactions, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             c.TransactionType.ToString().IndexOf(filterTransactions, System.StringComparison.OrdinalIgnoreCase) >= 0
                         select c).ToArray();
                }
            }
        }

        string filterTransactionLines;
        public string FilterTransactionLines
        {
            get { return filterTransactionLines; }
            set
            {
                filterTransactionLines = value;

                if (string.IsNullOrEmpty(filterTransactionLines))
                    this.TransactionLines = Transaction.Line;
                else
                {
                    this.TransactionLines =
                        (from c in Transaction.Line
                         where !string.IsNullOrEmpty(c.Description) && c.Description.IndexOf(filterTransactionLines, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             !string.IsNullOrEmpty(c.AccountID) && c.AccountID.IndexOf(filterTransactionLines, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             !string.IsNullOrEmpty(c.RecordID) && c.RecordID.IndexOf(filterTransactionLines, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             !string.IsNullOrEmpty(c.SourceDocumentID) && c.SourceDocumentID.IndexOf(filterTransactionLines, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             c.SystemEntryDate.ToString("yyyy-MM-ddTHH:mm:ss").IndexOf(filterTransactionLines, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                             c.ItemElementName.ToString().IndexOf(filterTransactionLines, System.StringComparison.OrdinalIgnoreCase) >= 0
                         select c).ToArray();
                }
            }
        }
    }
}
